<script>
/**
 * 头像上传表单域
 *    props: {
 *      url: 上传地址
 *      width: <int>px
 *      height: <int>px
 *      ...el-upload
 *    }
 */
export default {
  name: 'YmAvatarUploader',
  functional: true,
  render (h, context) {
    const { props, data } = context
    let child = (props.value) ? h('img', {
      class: 'avatar',
      style: {
        width: props.width || '178px',
        height: props.height || '178px'
      },
      props: {
        src: props.value
      }
    }) : h('i', {
      class: ['el-icon-plus', 'avatar-uploader-icon']
    })

    return h(
      'el-upload',
      {
        ...data,
        props: {
          ...data.props,
          ...props,
          action: props.url || '',
          showFileList: false,
          onSuccess: (response) => {
            if (context.listeners.change && response.url) {
              context.listeners.change(response.url)
            }
          }
        }
      },
      [child, h('div', props.label || '照片')]
    )
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  display: block;
}
</style>
